﻿using SnmpForm.SnmpWrapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace SnmpForm.ApBatchConfig
{
    class BatchUpdateSystemTask
    {
        FrmUpdateStatus _objForm = null;
        List<BusinessWrapper.Ap> _aps = null;
        private string m_strRequest = null;
        public BatchUpdateSystemTask(FrmUpdateStatus objForm, 
            List<BusinessWrapper.Ap> aps, 
            string strRequest)
        {
            _objForm = objForm;
            _aps = aps;
            m_strRequest = strRequest;
        }
        public void Run()
        {
            //发送升级请求
            SendRequest();

            //
            while (true)
            {
                GetStatus();
                Thread.Sleep(1000);
            }
        }
        private void SendRequest()
        {
            foreach (BusinessWrapper.Ap ap in _aps)
            {
                string strStatus = "";
                try
                {
                    ap.UpdateSystem(m_strRequest);
                    strStatus = "已请求升级";
                }
                catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strStatus = "超时";
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strStatus = "套接口异常";
                }
                catch (ApSnmpException ex)
                {
                    strStatus = ex.Message;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strStatus = "未知异常";
                }

                _objForm.BeginInvoke(_objForm.updateSystemStatus,
                    new Object[] { ap, strStatus }
                    );
            }
        }

        private void GetStatus()
        {
            //获取升级状态
            foreach (BusinessWrapper.Ap ap in _aps)
            {
                string strUpdateStatus = "";
                try
                {
                    strUpdateStatus = ap.ScanUpdateSystem();
                }
                catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strUpdateStatus = "获取状态失败，有可能是由于ap重启中";
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strUpdateStatus = "获取状态失败，有可能是由于ap重启中";
                }
                catch (ApSnmpException ex)
                {
                    strUpdateStatus = ex.Message;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    strUpdateStatus = "获取状态失败，有可能是由于ap重启中";
                }

                _objForm.BeginInvoke(_objForm.updateSystemStatus, 
                    new Object[] { ap, strUpdateStatus }
                    );
            }
        }
    }
}
